﻿<?php
// 信息
class InfoAction extends Action {
    
	public function index(){

		if(isset($_GET['aid'])){
			$aid = intval($_GET['aid']);
			$_SESSION['aid'] = $aid;
		}else{
			if(isset($_SESSION['aid'])) $aid = $_SESSION['aid'];
			else $aid=0;
		}

		//获取站点名称
		if(!empty($_SESSION['area'])){
			$area = $_SESSION['area'];
		}else{
			$area = getAreaName($aid);
		}

		if(isset($_GET['cid'])){
			$cid = intval($_GET['cid']);
			$_SESSION['cid'] = $cid;
		}else{
			if(isset($_SESSION['cid'])) $cid = $_SESSION['cid'];
			else $cid=0;
		}

		$catename = getCategoryName($cid);

		$name = $this->getActionName();
		$m = M($name);
		import('ORG.Util.Page');// 导入分页类
		
		$map=array('status'=>1);
		if($aid)$map['aid']=$aid;
		if($cid)$map['catid']=$cid;

		$count = $m->where($map)->count();// 查询满足要求的总记录数
		$Page = new Page($count,2);// 实例化分页类 传入总记录数
		$Page->rollPage = 5;	//最多显示20个分页
		
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$nowPage = isset($_GET['p'])?$_GET['p']:1;

		$list = $m->where($map)->order('create_time desc')->page($nowPage.','.$Page->listRows)->select();
		//ECHO $m->getLastSql();
		$Page->setConfig('theme','%upPage% %downPage% %prePage% %linkPage% %nextPage%');
		$page = $Page->show();// 分页显示输出
		//$page= trim($page);
		$category = getCategorys(13);

		$this->assign('aid',$aid);
		$this->assign('area',$area);
		$this->assign('catename',$catename);
		$this->assign('category',$category);
        $this->assign ( "page", $page );   
		$this->assign('list',$list);
		$this->display();
    }

	//显示详细信息
	public function show()
	{
		if(isset($_GET['aid'])){
			$aid = intval($_GET['aid']);
			$_SESSION['aid'] = $aid;
		}else{
			if(isset($_SESSION['aid'])) $aid = $_SESSION['aid'];
			else $aid=0;
		}

		$id = $this->_get('id');
		$m = M('Info');
		$new = $m->find($id);
		// $new['content'] = htmlspecialchars($new['content']);
		$this->assign('aid',$aid);
		$this->assign('new',$new);// 赋值数据集
		$this->display();
	}

	//发布信息
	public function write()
	{
		if(isset($_GET['aid'])){
			$aid = intval($_GET['aid']);
			$_SESSION['aid'] = $aid;
			cookie('aid',$aid,360000000); // 指定cookie保存时间
		}else{
			if(isset($_SESSION['aid'])) $aid = $_SESSION['aid'];
			else $aid=0;
		}

        //信息分类
        $type = getCategorys(13);
        $this->assign('type', $type);
		$this->assign('aid', $aid);
		$this->display('write');
	}

	//发布信息
	public function add()
	{
		$data = array();
		
		if( md5($this->_request('verify_code')) == session('mverify') )
		{
			if($_POST['title']){
	
	            $m=M('Info');
	            $m->create();
	            $m->create_time = date('Y-m-d H:i:s');
				if(!empty($_SESSION['aid']))$aid = $_SESSION['aid'];
				else $aid = intval($_REQUEST['aid']);
				$m->aid = $aid;
	            $result = $m->add();
	
	            if($result) {
	                $this->ajaxReturn(1,'发布成功',0);
	            }else{
	                $this->ajaxReturn(0,'内容添加错误',0);
	            }
	        }else{
	        	$this->ajaxReturn(1,'内容填写不全',0);
	        }
		}else{
			$this->ajaxReturn(0,'验证码错误',0);
		}
		
	}

}

